<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>博文管理</title>
		<link rel="stylesheet" href="/baseAdmin/pear/css/pear.css" />
	</head>
	<body class="pear-container">
		<!--条件搜索框-->
		<div class="layui-card">
			<div class="layui-card-body">
				<form class="layui-form" action="">
					<div class="layui-form-item">
						<div class="layui-form-item layui-inline">
							<label class="layui-form-label">日期</label>
							<div class="layui-input-block">
								<input type="text" name="date" readonly id="date" placeholder=" - " autocomplete="off" class="layui-input">
							</div>
						</div>

						<div class="layui-form-item layui-inline">
							<label class="layui-form-label">分类</label>
							<div class="layui-input-block">
								<select name="category" lay-filter="category" id="category">
									<option value=""></option>
									<option value="0" th:each="category:${categoryList}" th:value="${category.id}" th:text="${category.categoryName}">分类1</option>
								</select>
							</div>
						</div>
						<div class="layui-inline layui-form-item">
							<label class="layui-form-label">标题</label>
							<div class="layui-input-inline">
								<input type="text" name="title" autocomplete="off"
									   placeholder="请输入关键字" class="layui-input">
							</div>
						</div>
						<div class="layui-form-item layui-inline">
							<label class="layui-form-label">状态</label>
							<div class="layui-input-block">
								<select name="status" lay-filter="status" id="status">
									<option value=""></option>
									<option value="1">已发布</option>
									<option value="0">草稿</option>
								</select>
							</div>
						</div>

						<div class="layui-form-item layui-inline">
							<button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="user-query">
								<i class="layui-icon layui-icon-search"></i>
								查询
							</button>
							<button type="reset" class="pear-btn pear-btn-md">
								<i class="layui-icon layui-icon-refresh"></i>
								重置
							</button>
						</div>
					</div>
				</form>
			</div>
		</div>
		<!--数据表格-->
		<div class="layui-card">
			<div class="layui-card-body">
				<table id="article_table" lay-filter="article_table"></table>
			</div>
		</div>

		<!--新增/删除-->
		<script type="text/html" id="user-toolbar">
			<button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
		        <i class="layui-icon layui-icon-add-1"></i>
		        新增
		    </button>
		    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
		        <i class="layui-icon layui-icon-delete"></i>
		        删除
		    </button>
		</script>

		<!--表格操作-编辑/删除-->
		<script type="text/html" id="article-bar">
			<button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></button>
		    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i></button>
		</script>

		<!--将整型状态码转为字符串数据-->
		<script type="text/html" id="status0">
			{{#if (d.status == 0) { }}
			<span>已发布</span>
			{{# }else if(d.status == 1){ }}
			<span>草稿</span>
			{{# } }}
		</script>


		<!-- 日期 格式化 -->
		<script type="text/html" id="createTime">
			{{# if('id' in d && d.createTime) {  }}
			{{layui.util.toDateString(d.createTime, 'yyyy-MM-dd')}}
			{{# } }}
		</script>
		<script type="text/html" id="updateTime">
			{{# if('id' in d && d.updateTime) {  }}
			{{layui.util.toDateString(d.updateTime, 'yyyy-MM-dd')}}
			{{# } }}
		</script>

		<script src="/baseAdmin/layui/layui.js"></script>
		<script src="/baseAdmin/pear/pear.js"></script>
		<script src="/plugins/jquery/jquery-3.4.1.min.js"></script>
		<script src="/layuimini/js/lay-module/layuimini/miniTab.js" charset="utf-8"></script>
		<script>
			$(function() {
				/*初始化分类信息*/
			});

			layui.use(['table', 'form', 'jquery','common','util','laydate','miniTab'], function() {
				let table = layui.table;
				let layer = layui.layer;
				let form = layui.form;
				let util = layui.util;
				let miniTab = layui.miniTab;
				let laydate = layui.laydate;
				let $ = layui.jquery;
				let common = layui.common;

				//渲染日期组件
				laydate.render({
					elem: '#date',
					range: '~'
				});
				//创建表头
				let cols = [
						[
					{type: "checkbox", width: 50},
					{field: 'id', width: 80, title: 'ID', sort: true, hide: true},
					{field: 'articleTitle', minWidth: 120, title: '标题'},
					{field: 'status', width: 100, title: '状态', templet: '#status0',sort:true},
					{field: 'categoryName', width: 140, title: '分类'},
					{field: 'totalView', width: 100, title: '阅读量', sort: true},
					{field: 'commentNum', width: 100, title: '评论数', sort: true},
					{field: 'createTime', width: 140, templet: '#createTime', title: '创作时间', sort: true},
					{field: 'updateTime', width: 140, templet: '#updateTime', title: '更新时间', sort: true},
					{field:'isView', title:'是否公开', width:100,
						templet: function(d){
							if (d.view == 1) {
								return '<input type="checkbox" name="isView" value="'+d.id+'" checked lay-skin="switch" lay-text="公开|私密" lay-filter="changeView">'
							} else {
								return '<input type="checkbox" name="isView" value="'+d.id+'" lay-skin="switch" lay-text="公开|私密" lay-filter="changeView">'
							}
						}
					},
					{field:'isComment', title:'评论', width:100,
						templet: function(d){
							if (d.comment == 1) {
								return '<input type="checkbox" name="isComment" value="'+d.id+'" checked lay-skin="switch" lay-text="启用|禁止" lay-filter="changeComment">'
							} else {
								return '<input type="checkbox" name="isComment" value="'+d.id+'" lay-skin="switch" lay-text="启用|禁止" lay-filter="changeComment">'
							}
						}
					},
					{title: '操作', width: 150, toolbar: '#article-bar', align: "center"}
				]
				]
				//渲染表格
				table.render({
					elem: '#article_table',
					url: '/system/article/articleList',
					page: true,
					cols: cols,
					skin: 'line',
					toolbar: '#user-toolbar',
					method:"post",
					contentType: 'application/json',
					response: {
						statusCode: 200     //规定成功的状态码，默认：0
					},
					defaultToolbar: [{
						title: '刷新',
						layEvent: 'refresh',
						icon: 'layui-icon-refresh',
					}, 'filter', 'print', 'exports']
				});
				//是否可见按钮切换
				form.on('switch(changeView)', function(obj){
					layer.tips(this.value+":"+obj.elem.checked, obj.othis);
					$.ajax({
						url:"/system/article/changeView?isView="+obj.elem.checked+"&id="+this.value , // 请求路径
						type:"get" , //请求方式
						success:function (data) {
							layer.msg(data.message);
						},//响应成功后的回调函数
						error:function () {
							alert("出错啦...")
						},//表示如果请求响应出现错误，会执行的回调函数
					});
				});
				//评论功能按钮切换
				form.on('switch(changeComment)', function(obj){
					layer.tips(this.value+":"+obj.elem.checked, obj.othis);
					$.ajax({
						url:"/system/article/changeComment?isComment="+obj.elem.checked+"&id="+this.value , // 请求路径
						type:"get" , //请求方式
						success:function (data) {
							layer.msg(data.message);
						},//响应成功后的回调函数
						error:function () {
							alert("出错啦...")
						},//表示如果请求响应出现错误，会执行的回调函数
					});
				});

				table.on('tool(article_table)', function(obj) {
					if (obj.event === 'remove') {
						window.remove(obj);
					} else if (obj.event === 'edit') {
						window.edit(obj);
					}
				});
				table.on('toolbar(article_table)', function(obj) {
					if (obj.event === 'add') {
						window.add();
					} else if (obj.event === 'refresh') {
						window.refresh();
					} else if (obj.event === 'batchRemove') {
						window.batchRemove(obj);
					}
				});

				form.on('submit(user-query)', function(data) {
					table.reload('article_table', {
						where: data.field
					})
					return false;
				});
				form.on('switch(user-enable)', function(obj) {
					layer.tips(this.value + ' ' + this.name + '：' + obj.elem.checked, obj.othis);
				});

				window.add = function() {
					// 打开新的窗口
					miniTab.openNewTabByIframe({
						href:"/system/toArticleUpload",
						title:"发布博客",
					});
				}

				window.edit = function(obj) {
					// 打开新的窗口
					miniTab.openNewTabByIframe({
						href:"/system/toArticleEdit?id="+obj.data.id,
						title:"编辑博客",
					});
				}
				window.remove = function(obj) {
					layer.confirm('确定要删除该文章,可在回收站找回', {icon: 3, title: '提示'}, function(index) {
						layer.close(index);
						let loading = layer.load();
						$.ajax({
							url:"/system/article/toRecycleBinByIds?ids="+obj.data.id , // 请求路径
							type:"get" , //请求方式
							success:function (data) {
								layer.close(loading);
								if (data.code == 200) {
									table.reload('article_table');
								} else {
									layer.msg(data.message);
								}
							},//响应成功后的回调函数
							error:function () {
								alert("出错啦...")
							},//表示如果请求响应出现错误，会执行的回调函数
						});
					});
				}
				window.batchRemove = function(obj) {
					var checkIds = common.checkField(obj,'id');
					if (checkIds === "") {
						layer.msg("未选中数据", {
							icon: 3,
							time: 1000
						});
						return false;
					}
					layer.confirm('确定要删除这些文章吗', {icon: 3, title: '提示'}, function(index) {
						layer.close(index);
						let loading = layer.load();
						$.ajax({
							url:"/system/article/toRecycleBinByIds?ids="+checkIds , // 请求路径
							type:"get" , //请求方式
							success:function (res) {
								layer.close(loading);
								if (res.code == 200) {
									table.reload('article_table');
								} else {
									layer.msg(res.message);
								}
							},//响应成功后的回调函数
							error:function () {
								alert("出错啦...")
							},//表示如果请求响应出现错误，会执行的回调函数
						});
					});
				}
				window.refresh = function(param) {
					table.reload('article_table');
				}

			})
		</script>
	</body>
</html>
